package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.ValidateFileTypeUtil;
import io.renren.common.utils.excel.ExcelUtils;
import io.renren.modules.generator.entity.VehicleEntity;
import io.renren.modules.generator.export.VehicleExcelDataCreaterImpl;
import io.renren.modules.generator.service.VehicleService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-29 10:08:36
 */
@RestController
@RequestMapping("generator/vehicle")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private VehicleExcelDataCreaterImpl vehicleExcelDataCreater;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        Query query = new Query(params);

        List<VehicleEntity> list = vehicleService.queryPage(query);

        int total =vehicleService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			VehicleEntity vehicle = vehicleService.selectById(id);

        return R.ok().put("vehicle", vehicle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:vehicle:save")
    public R save(@RequestBody VehicleEntity vehicle){
			vehicleService.insert(vehicle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:vehicle:update")
    public R update(@RequestBody VehicleEntity vehicle){
			vehicleService.updateById(vehicle);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:vehicle:delete")
    public R delete(@RequestBody Integer[] ids){
			vehicleService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    @GetMapping("/queryAllVehicles")
    public R queryAllVehicles(){
        List<VehicleEntity> vehicleEntities = vehicleService.selectList(null);
        return R.ok().put("data",vehicleEntities);
    }
    @GetMapping("export")
    @RequiresPermissions("generator:vehicle:export")
    public void exportDate(@RequestParam Map<String, Object> params, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        String[] lableNames = {"车辆代码","车辆名称","内径长","内径宽","内径高","外径长","外径宽","外径高","体积","载重","板数","备注","预留字段1","预留字段2","预留字段3"};
        String[] columnNames={"code","name","inLength","inWidth","inHeigth","outLength","outWidth","outHeigth","volume","weight","numberOfplates","remark","extendFirst","extendSecond","extendThird"};
        excelUtils.export(response,"数据导出.xlsx",lableNames,columnNames,params,vehicleExcelDataCreater);

    }


    /**
     * 车辆导入
     */
    @PostMapping("/import")
    @RequiresPermissions("generator:vehicle:import")
    public R importFile(@RequestParam("file") MultipartFile file){

        Workbook book = ValidateFileTypeUtil.validate(file);
        if(book == null){
            return R.error();
        }
        try {
            return vehicleService.importFile(book);
        }catch (DuplicateKeyException d){
            return R.error(d.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


}
